package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go-mall/pkg/util"
	"go-mall/service"
	"net/http"
	"strconv"
)

func CreateFavorite(c *gin.Context) {
	claims, _ := util.ParseToken(c.GetHeader("Authorization"))
	createFavoriteService := service.FavoriteService{}
	if err := c.ShouldBind(&createFavoriteService); err == nil {
		res := createFavoriteService.Create(c.Request.Context(), claims.ID)
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}

func ListFavorite(c *gin.Context) {
	claims, _ := util.ParseToken(c.GetHeader("Authorization"))
	listFavoriteService := service.FavoriteService{}
	if err := c.ShouldBind(&listFavoriteService); err == nil {
		res := listFavoriteService.List(c.Request.Context(), claims.ID)
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}

func DeleteFavorite(c *gin.Context) {
	claims, _ := util.ParseToken(c.GetHeader("Authorization"))
	deleteFavoriteService := service.FavoriteService{}
	pId, _ := strconv.ParseUint(c.Param("id"), 10, 64)
	res := deleteFavoriteService.Delete(c.Request.Context(), uint(pId), claims.ID)
	c.JSON(http.StatusOK, res)
	//if err := c.ShouldBind(&deleteFavoriteService); err == nil {
	//	uId, _ := strconv.ParseUint(c.Param("id"), 10, 64)
	//	res := deleteFavoriteService.Delete(c.Request.Context(), uint(uId), claims.ID)
	//	c.JSON(http.StatusOK, res)
	//} else {
	//	fmt.Println("Error", err.Error())
	//	c.JSON(http.StatusBadRequest, err)
	//}
}
